home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 May / EnigmA AMIGA RUN 18 (1997)(G.R. Edizioni)(IT)[!][issue 1997-05][EAR-CD II].iso / earcd / util / sys / msys_1_2.lha / Src / avail.c next >
C/C++ Source or Header  |  1996-02-10  |  8KB  |  250 lines

  1. /*
  2. **    avail.c - report memory statistics
  3. **    $VER: avail.c 42.1 (10.2.96)
  4. **    Copyright © 1996 Michal Letowski
  5. **
  6. **    42.1 (10.2.96) - initial version
  7. */
  8.  
  9. #define __USE_SYSBASE
  10.  
  11. #include <exec/types.h>
  12. #include <exec/execbase.h>
  13. #include <exec/libraries.h>
  14. #include <exec/memory.h>
  15. #include <dos/dos.h>
  16. #include <dos/rdargs.h>
  17. #include <support/types.h>
  18. #include <support/dos.h>
  19.  
  20. #include <proto/exec.h>
  21. #include <proto/dos.h>
  22.  
  23. #include "avail.rev.h"
  24.  
  25.  
  26. /*
  27. **    Constants
  28. */
  29. #define DOS_NAME        "dos.library"
  30. #define DOS_VERN        37L
  31.  
  32. #define TEMPLATE        "CHIP/S,FAST/S,VIRTUAL/S,TOTAL/S,"\
  33.                                         "FREE/S,INUSE/S,MAXIMUM/S,LARGEST/S,FLUSH/S"
  34.  
  35. #define BIG_SIZE        0x7FFFFFF0                                    /* Memory allocation size */
  36. #define ALLOC_CNT        16                                                    /* Number of attempts */
  37.  
  38. /* Memory types */
  39. #define MT_CHIP            0
  40. #define MT_FAST            1
  41. #define MT_VIRT            2
  42. #define MT_TOTAL        3
  43. #define MT_COUNT        4
  44.  
  45. /* Amount types */
  46. #define AT_FREE            0
  47. #define AT_INUSE        1
  48. #define AT_MAXIMUM    2
  49. #define AT_LARGEST    3
  50. #define AT_COUNT        4
  51.  
  52. /* Find maximum of three numbers */
  53. #define Max3(a,b,c)    ((a)>(b) ? ((a)>(c) ? (a) : (c)) : ((b)>(c) ? (b) : (c)))
  54.  
  55.  
  56. /*
  57. **    Private structures
  58. */
  59. struct Options
  60. {
  61.     LBOOL  opt_Chip;                                                            /* HARD/S */
  62.     LBOOL  opt_Fast;                                                            /* SOFT/S */
  63.     LBOOL  opt_Virtual;                                                        /* FORCE/S */
  64.     LBOOL  opt_Total;                                                            /* TOTAL/S */
  65.     LBOOL  opt_Free;                                                            /* FREE/S */
  66.     LBOOL  opt_InUse;                                                            /* INUSE/S */
  67.     LBOOL  opt_Maximum;                                                        /* MAXIMUM/S */
  68.     LBOOL  opt_Largest;                                                        /* LARGEST/S */
  69.     LBOOL  opt_Flush;                                                            /* FLUSH/S */
  70. };    /* Options */
  71.  
  72.  
  73. /*
  74. **    Global data
  75. */
  76. STATIC CONST TEXT VersionString[]=
  77.     VERSION(PROG_NAME,PROG_VERSION,PROG_REVISION,PROG_DATE);
  78.  
  79.  
  80. /*
  81. **    Private functions prototypes
  82. */
  83. STATIC VOID GetMemoryAmount(ULONG *amnt, ULONG memType);
  84.  
  85.  
  86. /*
  87. **    Public functions
  88. */
  89. /****** C/Avail **********************************************************
  90. *
  91. *   NAME
  92. *       Avail -- report memory statistics. (V42)
  93. *
  94. *   SYNOPSIS
  95. *       Avail [CHIP] [FAST] [VIRTUAL] [TOTAL]
  96. *             [FREE] [INUSE] [MAXIMUM] [LARGEST] [FLUSH]
  97. *
  98. *   TEMPLATE
  99. *       Avail "CHIP/S,FAST/S,VIRTUAL/S,TOTAL/S,
  100. *              FREE/S,INUSE/S,MAXIMUM/S,LARGEST/S,FLUSH/S"
  101. *
  102. *   FUNCTION
  103. *       Avail examines memory usage and reports statistics back to the
  104. *       user. Avail can print full (default) or just selected statistics.
  105. *       Avail can report amount of free memory, amount of memory in use,
  106. *       amount of total memory or size of largest contiguous memory block with
  107. *       respect to the memory types. If FLUSH option is used, Avail will try
  108. *       to free as much memory taken by system as possible. You can't use
  109. *       neither CHIP, FAST, VIRTUAL, TOTAL nor FREE, INUSE, MAXIMUM, LARGEST
  110. *       options together. All values are in bytes.
  111. *
  112. *   INPUTS
  113. *       CHIP    - amount of chip memory will be printed.
  114. *       FAST    - amount of fast memory will be printed.
  115. *       VIRTUAL - amount of virtual memory will be printed.
  116. *       TOTAL   - amount of total (i.e. fast, chip and virtual) memory will
  117. *                 be printed (default).
  118. *       FREE    - amount of free memory will be printed (default).
  119. *       INUSE   - amount of memory in use will be printed.
  120. *       MAXIMUM - amount of free and in use memory will be printed.
  121. *       LARGEST - size of largest contiguous memory block will be printed.
  122. *       FLUSH   - if this option is used, Avail will try to free as much
  123. *                 memory taken by system as possible, before printing
  124. *                 statistics.
  125. *
  126. *   RESULT
  127. *       RETURN_FAIL  - if 'dos.library' couldn't be opened.
  128. *       RETURN_ERROR - if command line arguments couldn't be processed or
  129. *                      unallowed combination of options appeared.
  130. *       RETURN_OK    - if statistics were printed.
  131. *
  132. *   EXAMPLE
  133. *       Avail
  134. *           ; Prints all memory statistics
  135. *       Avail CHIP
  136. *           ; Prints amount of free chip memory.
  137. *       Avail TOTAL FLUSH
  138. *           ; Frees some system memory and prints total amount of free memory.
  139. *       Avail VIRTUAL LARGEST
  140. *           ; Prints size of largest contiguous virtual memory block
  141. *
  142. *   NOTES
  143. *       Memory is flushed through several attempts to allocate 2GB of memory.
  144. *       Avail is pure and can be made resident.
  145. *
  146. *   BUGS
  147. *       Virtual memory statistics were not tested.
  148. *
  149. *   SEE ALSO
  150. *       exec.library/AvailMem().
  151. *
  152. *****************************************************************************
  153. *
  154. */
  155. LONG Avail(VOID)
  156. {
  157.     struct ExecBase *SysBase=INITSYSBASE;
  158.     struct Library *DOSBase;
  159.  
  160.     ULONG Amnt[MT_COUNT][AT_COUNT];
  161.     struct Options Opts;
  162.     struct RDArgs *Args;
  163.     LONG *OptPtr;
  164.     LONG I,MT=0,AT=0,RC=RETURN_FAIL;
  165.     
  166.     /* Open DOS */
  167.     unless(DOSBase=OpenLibrary(DOS_NAME,DOS_VERN))
  168.         throw2(SetResult2(ERROR_INVALID_RESIDENT_LIBRARY),    NO_DOS);
  169.  
  170.     RC=RETURN_ERROR;                                                            /* Working a bit... */
  171.  
  172.     /* Read arguments */
  173.     clear(&Opts);                                                                    /* Clear out buffer */
  174.     unless(Args=ReadArgs(TEMPLATE,(LONG *)&Opts,NULL))
  175.         throw2(PrintFault(IoErr(),PROG_NAME),                                NO_ARGS);
  176.  
  177.     /* Check options correctness */
  178.     for(OptPtr=&Opts.opt_Chip; OptPtr<&Opts.opt_Free; OptPtr++)
  179.         if(*OptPtr)    MT++;
  180.     for(OptPtr=&Opts.opt_Free; OptPtr<&Opts.opt_Flush; OptPtr++)
  181.         if(*OptPtr)    AT++;
  182.     if(MT>1 || AT>1)
  183.         throw2(CauseIoErr(ERROR_TOO_MANY_ARGS,PROG_NAME),        BAD_ARGS);
  184.  
  185.     RC=RETURN_OK;                                                                    /* Success */
  186.  
  187.     /* First: flush memory if needed */
  188.     if(Opts.opt_Flush)                                                        /* Flushing requested? */
  189.         for(I=0; I<ALLOC_CNT; I++)                                    /* Try several times to... */
  190.             FreeVec(AllocVec(BIG_SIZE,MEMF_PUBLIC));    /* ... allocate/free 2 GB */
  191.  
  192.     /* Process amounts */
  193.     Forbid();
  194.     GetMemoryAmount(Amnt[MT_CHIP],MEMF_CHIP);
  195.     GetMemoryAmount(Amnt[MT_FAST],MEMF_FAST);
  196.     GetMemoryAmount(Amnt[MT_VIRT],MEMF_PUBLIC);
  197.     GetMemoryAmount(Amnt[MT_TOTAL],MEMF_ANY);
  198.     Permit();
  199.     for(I=0; I<AT_COUNT; I++)                                            /* Calculate amount of VMEM */
  200.         Amnt[MT_VIRT][I]=Amnt[MT_TOTAL][I]-Amnt[MT_VIRT][I];
  201.     Amnt[MT_TOTAL][AT_LARGEST]=Max3(Amnt[MT_CHIP][AT_LARGEST],
  202.                                                                     Amnt[MT_FAST][AT_LARGEST],
  203.                                                                     Amnt[MT_VIRT][AT_LARGEST]);
  204.  
  205.     if(Opts.opt_Chip)                MT=MT_CHIP;
  206.     elif(Opts.opt_Fast)            MT=MT_FAST;
  207.     elif(Opts.opt_Virtual)    MT=MT_VIRT;
  208.     elif(Opts.opt_Total)        MT=MT_TOTAL;
  209.     else                                        MT=-1;
  210.  
  211.     if(Opts.opt_Free)                AT=AT_FREE;
  212.     elif(Opts.opt_InUse)        AT=AT_INUSE;
  213.     elif(Opts.opt_Maximum)    AT=AT_MAXIMUM;
  214.     elif(Opts.opt_Largest)    AT=AT_LARGEST;
  215.     else                                        AT=-1;
  216.  
  217.     if(MT<0 && !(AT<0))    MT=MT_TOTAL;
  218.     elif(AT<0)                    AT=AT_FREE;
  219.     if(MT<0)                                                                            /* No memory type specified */
  220.     {                                                                                            /* Do full report */
  221.         VPrintf("Type  Available    In-Use   Maximum   Largest\n",NULL);
  222.         VPrintf("CHIP %10lu %9lu %9lu %9lu\n",Amnt[MT_CHIP]);
  223.         VPrintf("FAST %10lu %9lu %9lu %9lu\n",Amnt[MT_FAST]);
  224.         VPrintf("VIRT %10lu %9lu %9lu %9lu\n",Amnt[MT_VIRT]);
  225.         VPrintf("Total %9lu %9lu %9lu %9lu\n",Amnt[MT_TOTAL]);
  226.     }
  227.     else                                                                                    /* Type specified */
  228.         VPrintf("%lu\n",&Amnt[MT][AT]);                            /* Print amount */
  229.             
  230.     /* Exceptions */
  231.     catch(BAD_ARGS,    );
  232.     catch(NO_ARGS,    FreeArgs(Args));
  233.     catch(NO_DOS,        CloseLibrary(DOSBase));
  234.     return(RC);
  235. }    /* Avail */
  236.  
  237.  
  238. /*
  239. **    Private functions
  240. */
  241. STATIC VOID GetMemoryAmount(ULONG *amnt, ULONG memType)
  242. {
  243.     struct ExecBase *SysBase=INITSYSBASE;
  244.  
  245.     amnt[AT_MAXIMUM]=    AvailMem(memType | MEMF_TOTAL);
  246.     amnt[AT_LARGEST]=    AvailMem(memType | MEMF_LARGEST);
  247.     amnt[AT_FREE]=        AvailMem(memType);
  248.     amnt[AT_INUSE]=        amnt[AT_MAXIMUM]-amnt[AT_FREE];
  249. }    /* GetMemoryAmount */
  250.